package com.health.viewscontrols.tjbg.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.RequiresApi;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.AdaptScreenUtils;
import com.nova.view.NovaProgressDialog;
import com.nova.view.NovaTextDialog;

/**
 * 基本类
 * 
 * @author nova
 *
 */
public abstract class NovaPtActivity extends Activity implements NovaIBaseView {

	protected View mContentView;
	protected Activity mActivity;

	/**
	 * 上次点击时间
	 */
	private long lastClick = 0;

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mActivity = this;
		this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		this.getWindow().setStatusBarColor(0xff000000);
		Bundle bundle = getIntent().getExtras();
		initData(bundle);
		setBaseView(bindLayout());
		initView(savedInstanceState, mContentView);
		doBusiness();
	}

	@SuppressLint("ResourceType")
	protected void setBaseView(@LayoutRes int layoutId) {
		if (layoutId <= 0) return;

		setContentView(mContentView = LayoutInflater.from(this).inflate(layoutId, null));
	}

	/**
	 * 判断是否快速点击
	 *
	 * @return {@code true}: 是<br>{@code false}: 否
	 */
	private boolean isFastClick() {
		long now = System.currentTimeMillis();
		if (now - lastClick >= 200) {
			lastClick = now;
			return false;
		}
		return true;
	}

	@Override
	public void onClick(final View view) {
		if (!isFastClick()) onWidgetClick(view);
	}

	/**
	 * 视图点击事件
	 *
	 * @param view 视图
	 */
	public void onWidgetClick(final View view) {

	}

	@Override
	public Resources getResources() {
		return AdaptScreenUtils.adaptWidth(super.getResources(), 840);
	}

	private NovaProgressDialog progressDialog;
	/**
	 * 显示等待框(不同颜色)
	 */
	public void showProgressDialogBuilder(SpannableStringBuilder builder) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setTextBuilder(builder);
		this.progressDialog.setCancelable(false);
		this.progressDialog.show();
	}

	/**
	 * 显示等待框
	 */
	public void showProgressDialog(String text) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(false);
		this.progressDialog.show();
	}

	/**
	 * 显示对话框，boolean是否可以取消
	 */
	public void showProgressDialog(String text, boolean cancelable) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(cancelable);
		this.progressDialog.show();
	}

	public void showProgressDialog(int text) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(false);
		this.progressDialog.show();
	}

	public void showProgressDialog(int text, boolean cancelable) {
		if (this.progressDialog == null)
			this.progressDialog = new NovaProgressDialog(this);
		this.progressDialog.setText(text);
		this.progressDialog.setCancelable(cancelable);
		this.progressDialog.show();
	}

	/**
	 * 取消等待框
	 */
	public void cancelProgressDialog() {
		try {
			if (this.progressDialog != null) {
				this.progressDialog.setCancelable(false);
				this.progressDialog.cancel();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private NovaTextDialog textDialog;

	/**
	 * 显示文本信息
	 */
	public void showTextDialog(String text) {
		if (this.textDialog == null)
			this.textDialog = new NovaTextDialog(this);
		this.textDialog.setText(text);
		this.textDialog.show();
	}

	/**
	 * 显示数字信息
	 */
	public void showTextDialog(int text) {
		if (this.textDialog == null)
			this.textDialog = new NovaTextDialog(this);
		this.textDialog.setText(text);
		this.textDialog.show();
	}
	/**
	 * 显示文本信息，显示完毕后关闭Activity
	 */
	public void showTextDialog(String text,Activity activity) {
		if (this.textDialog == null)
			this.textDialog = new NovaTextDialog(this);
		this.textDialog.setFinishActivity(activity);
		this.textDialog.setText(text);
		this.textDialog.show();
	}

	public void finish() {
		cancelTextDialog();
		if (this.progressDialog != null)
			this.progressDialog.cancelImmediately();
		if (textDialog!=null) {
			textDialog.cancel();
			textDialog = null;
		}
		super.finish();
	}

	/**
	 * 取消文本信息对话框
	 */
	public void cancelTextDialog() {
		if (this.textDialog != null)
			this.textDialog.cancel();
	}

}